var nieuweLeerling = {};

nieuweLeerling.build = function() {

   Ti.Media.vibrate([0, 35]);
   
   var win = Ti.UI.createWindow({
       backgroundColor: '#fff',
       layout: 'vertical',
       navBarHidden:true,
   });

	var header = Ti.UI.createView({
	   width: Ti.UI.FILL,
	   height: '85dip',
	   backgroundImage: '/images/header.png',
	   top: 0
	});
	var title = Ti.UI.createLabel({
	    color: '#fff',
	    font: {fontSize:'20dip' },
	    top: '18dip',
	})
    title.text = 'Nieuwe leerling toevoegen';
	
	if(Ti.Platform.name != 'android')
    {
        var backButton = Ti.UI.createButton({
            title: 'Terug', 
            left: '10dip',
            top: '10dip',
            width: Ti.UI.SIZE,
            height: Ti.UI.SIZE,
            color: '#090C16',    
            visible: true
        });
        
        backButton.addEventListener('click', function() {
            win.close;
        });
        
        header.add(backButton);
  	}
	
	header.add(title);
	win.add(header);
	
	win.open();
	
	var nieuweLeerlingPlaceholder = Ti.UI.createScrollView({
	   height: Ti.UI.FILL,
	   width: '300dip',
	   layout: 'vertical'
	});
	
	var naamTextBox = Ti.UI.createTextField({
	   hintText: 'Naam',
	   width: Ti.UI.FILL,
	   height: '60dip',
	   borderColor: '#000000',
	   borderWidth: '1',
	   backgroundColor: Globals.TFBackgroundColor
	});
	
	nieuweLeerlingPlaceholder.add(naamTextBox);
	
	var groepTextBox = Ti.UI.createTextField({
	   top: '10dip',
	   hintText: 'Groep',
	   width: Ti.UI.FILL,
	   height: '60dip',
	   borderColor: '#000000',
	   borderWidth: '1',
	   backgroundColor: Globals.TFBackgroundColor
	});
	
	nieuweLeerlingPlaceholder.add(groepTextBox);
	
	var email1TextBox = Ti.UI.createTextField({
	   top: '10dip',
	   hintText: 'E-mail adres 1',
	   width: Ti.UI.FILL,
	   height: '60dip',
	   borderColor: '#000000',
	   borderWidth: '1',
	   backgroundColor: Globals.TFBackgroundColor
	});
	
	nieuweLeerlingPlaceholder.add(email1TextBox);
	
	var email2TextBox = Ti.UI.createTextField({
	   top: '10dip',
	   hintText: 'E-mail adres 2',
	   width: Ti.UI.FILL,
	   height: '60dip',
	   borderColor: '#000000',
	   borderWidth: '1',
	   backgroundColor: Globals.TFBackgroundColor
	});
	
	nieuweLeerlingPlaceholder.add(email2TextBox);
	
	var opslaanButton = Ti.UI.createButton({
	      top: '10dip',
	      backgroundImage: '/images/button_bg.png',
	      right: 0,
	      width: '150dip',
	      height: '60dip',  
	      bottom: '10dip',
	      color: '#fff',
	      font: {fontSize:'18dip' },
	      title: 'Opslaan'
	});
	
	var leerling;
	
	opslaanButton.addEventListener('click', function(e){
		
		Ti.Media.vibrate([0, 35]);
		
		
			naamTextBox.blur();
	    	groepTextBox.blur();
	    	email1TextBox.blur();
	    	email2TextBox.blur();
		var checkname = leerlingen.checkName(naamTextBox);
		   //Opslaan
		var checkmailone = leerlingen.emailChecker(email1TextBox);
		var checkmailtwo = leerlingen.emailChecker(email2TextBox);
		
		if(checkmailone && checkmailtwo && checkname) {
			id = Globals.newId;
				   
			leerling = new leerlingObject(id, naamTextBox.value, groepTextBox.value, email1TextBox.value, email2TextBox.value);
			var tableViewRow = Ti.UI.createTableViewRow({title:leerling.naam, leerling: leerling, color: '#000', height: '60dip',  font: {fontSize:'16dip' }});
			   
			
			tableViewRow.rightImage= '/images/pencil_edit.png';
			leerlingen.studentsTableView.appendRow(tableViewRow);
			Globals.leerlingenArray.push(leerling);
			
			
			var newid = parseInt(Globals.newId, 10) + 1;
			Globals.newId = newid + "";
			   
			AddLeerlingToXML();
			
			win.close();
		}
		else {
			naamTextBox.addEventListener('change', leerlingen.checkNameEvent);
			email1TextBox.addEventListener('change', leerlingen.emailCheckerEvent);
			email2TextBox.addEventListener('change', leerlingen.emailCheckerEvent);
			
			errorstr = [];
			var msg = "";
			//check what's wrong
			if(!checkmailone && email1TextBox.value != "") {
				errorstr.push("'" + email1TextBox.hintText + "'");
			}
			if(!checkmailtwo && email2TextBox.value != "") {
				errorstr.push("'" + email2TextBox.hintText + "'");
			}
			if(naamTextBox.value == "") {
				msg += "'" + naamTextBox.hintText + "' is niet ingevuld! ";
				if(errorstr.length > 0) {
					msg += "\n";
				}
			}
			
			//Ultimate Error Message builder
			switch(errorstr.length) {
				case 0:
					Ti.UI.createAlertDialog({
					    message: msg,
					    ok: 'OK',
					    title: 'Foutmelding'
					}).show();
					break;
				case 1:
					Ti.UI.createAlertDialog({
					    message: msg + errorstr[0] + " is niet geldig!",
					    ok: 'OK',
					    title: 'Foutmelding'
					}).show();
					break;
				case 2:
					Ti.UI.createAlertDialog({
					    message: msg + errorstr[0] + " en " + errorstr[1] +" zijn niet geldig! \n",
					    ok: 'OK',
					    title: 'Foutmelding'
					}).show();
					break;
				default:
					for(var i = 0; i<errorstr.length - 2;i++) {
						msg += errorstr[i] + ", ";
					}
					Ti.UI.createAlertDialog({
						    message: msg + errorstr[(errorstr.length - 2)] + " en " + errorstr[(errorstr.length - 1)] +" zijn niet geldig!",
						    ok: 'OK',
						    title: 'Foutmelding'
					}).show();
					break;
			}
			
		}
		
	});
	
	nieuweLeerlingPlaceholder.add(opslaanButton);
	
	win.add(nieuweLeerlingPlaceholder);
	
	function AddLeerlingToXML() {
        var target = Globals.getSupportFile('leerlingen.xml');
        
        if(target.exists()) {
            var xmltext = target.read().text;
            var doc = Ti.XML.parseString(xmltext);
            
                
            var newChild = doc.createElement("leerling");
            newChild.setAttribute("leerlingid", leerling.id);
            newChild.setAttribute("klas", escape(leerling.klas));
            newChild.setAttribute("naam", escape(leerling.naam));
            newChild.setAttribute("email", escape(leerling.mailadres + ";" + leerling.andermailadres));
            
            doc.documentElement.appendChild(newChild);
            target.write(Ti.XML.serializeToString(doc));
		
		}
	}
}